package com.songmao.pricecloudapitest.testcase.usermanagement;

import com.songmao.pricecloudapitest.requestcore.ResponePoJo;
import com.songmao.pricecloudapitest.server.PriceCloudHttpCore;
import com.songmao.pricecloudapitest.utils.AssertCore;
import com.songmao.pricecloudapitest.utils.ParsingJson;
import com.songmao.pricecloudapitest.utils.TestData;
import com.songmao.pricecloudapitest.utils.TestUitl;
import org.testng.annotations.Test;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Remy
 * @description : 报价用户/后台用户相关测试用例
 * @date 2020-07-10 16:04
 */
public class QuotebackendUsersTestCase extends PriceCloudHttpCore {

    private static String randomString = TestUitl.randomString(100);

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "新增报价用户")
    public void addPirceUser(Map<String, String> map) {
        String json;
        json = map.get("json").replaceAll("#", randomString);
        if (map.get("maxString") != null) {
            json = map.get("json").replaceAll("#", TestUitl.randomString(Integer.parseInt(map.get("maxString"))));
        }
        json = ParsingJson.update(json, "//appid", TestUitl.getAttribute("appid"));
        ResponePoJo pojo = super.post(map.get("url"), json);
        String code = pojo.getCode();
        AssertCore.assertEquals(200, pojo.getStatus());
        AssertCore.assertEquals(code, map.get("expected"));

        if (map.get("businessDependence") != null) {
            TestUitl.setAttribute("priceUserId", Objects.requireNonNull(ParsingJson.getPathCode(pojo.getData().toString(), map.get("businessDependence"))).toString());
        }

    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "编辑报价用户")
    public void editPriceUser(Map<String, String> map) {
        String url = TestUitl.replaceAccessToken(map.get("url"), "$", TestUitl.getAttribute("priceUserId").toString());

        String json = ParsingJson.update(map.get("json").replaceAll("#", TestUitl.randomString(100)), "//appid",
                TestUitl.getAttribute("appid"));
        ResponePoJo pojo = put(url, json);
        AssertCore.assertEquals(200, pojo.getStatus());

        AssertCore.assertEquals(map.get("expected"), pojo.getCode());
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "重置报价用户密码")
    public void resetPriceUserPassword(Map<String, String> map) {
        String url = TestUitl.replaceAccessToken(map.get("url"), "$", TestUitl.getAttribute("priceUserId").toString());
        ResponePoJo pojo = get(url);
        AssertCore.assertEquals(200, pojo.getStatus());
        AssertCore.assertEquals(map.get("expected"), pojo.getCode());
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "新增后台用户")
    public void addBackendUser(Map<String, String> map) {
        String json;
        json = map.get("json").replaceAll("#", randomString);
        if (map.get("maxString") != null) {
            json = map.get("json").replaceAll("#", TestUitl.randomString(Integer.parseInt(map.get("maxString"))));
        }
        json = ParsingJson.update(json, "//appid", TestUitl.getAttribute("appid"));
        ResponePoJo pojo = super.post(map.get("url"), json);
        String code = pojo.getCode();
        AssertCore.assertEquals(200, pojo.getStatus());
        AssertCore.assertEquals(code, map.get("expected"));

        if (map.get("businessDependence") != null) {
            TestUitl.setAttribute("backendUserId", Objects.requireNonNull(ParsingJson.getPathCode(pojo.getData().toString(), map.get("businessDependence"))).toString());
        }

    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "重置报价用户密码")
    public void resetBackendUserPassword(Map<String, String> map) {
        String url = TestUitl.replaceAccessToken(map.get("url"), "$", TestUitl.getAttribute("priceUserId").toString());
        ResponePoJo pojo = get(url);
        AssertCore.assertEquals(200, pojo.getStatus());
        AssertCore.assertEquals(map.get("expected"), pojo.getCode());
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "编辑后台用户")
    public void editBanckendUser(Map<String, String> map) {
        String url = TestUitl.replaceAccessToken(map.get("url"), "$", TestUitl.getAttribute("backendUserId").toString());

        String json = ParsingJson.update(map.get("json").replaceAll("#", TestUitl.randomString(100)), "//appid",
                TestUitl.getAttribute("appid"));
        ResponePoJo pojo = put(url, json);
        AssertCore.assertEquals(200, pojo.getStatus());

        AssertCore.assertEquals(map.get("expected"), pojo.getCode());
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "测试获取报价用户列表")
    public void priceUserListPage(Map<String, String> map) {
        String url = TestUitl.replaceAccessToken(map.get("url"), "$", TestUitl.getAttribute("appid").toString());
        ResponePoJo pojo = get(url);
        List<String> uuidList = (List<String>) (List) ParsingJson.getPathCodes(pojo.getData().toString(), map.get("expectedPath"));
        AssertCore.assertEquals(200, pojo.getStatus());
        assert uuidList != null;
        AssertCore.assertTrue(uuidList.size() <= Integer.parseInt(map.get("expected")));
    }

    @Test(dataProvider = "dataProviderMap", dataProviderClass = TestData.class, description = "测试获取报价后台列表")
    public void backendUserListPage(Map<String, String> map) {
        String url = TestUitl.replaceAccessToken(map.get("url"), "$", TestUitl.getAttribute("appid").toString());
        ResponePoJo pojo = get(url);
        List<String> uuidList = (List<String>) (List) ParsingJson.getPathCodes(pojo.getData().toString(), map.get("expectedPath"));
        AssertCore.assertEquals(200, pojo.getStatus());
        assert uuidList != null;
        AssertCore.assertTrue(uuidList.size() <= Integer.parseInt(map.get("expected")));
    }

}
